﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Food
{
    public class UserFoodFavs:IUserFoodFavs
    {
        public void AddFoodToUserFavs(int userId, int foodId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserFoodFav fav = new Model.UserFoodFav();
            fav.FoodID = foodId;
            fav.UserID = userId;
            context.UserFoodFavs.AddObject(fav);
            context.SaveChanges();
        }

        public void RemoveFoodFromUserFavs(int userId, int foodId, ref Model.NutriLeafDBEntities context)
        {
            if (context.UserFoodFavs.Where(p => p.UserID == userId && p.FoodID == foodId).Count() != 0) {
                context.UserFoodFavs.DeleteObject(context.UserFoodFavs.Where(p => p.UserID==userId && p.FoodID == foodId).First());
                context.SaveChanges();
            }
        }

        public void RemoveFoodFromUserFavs(int userFoodFavsId, ref Model.NutriLeafDBEntities context)
        {
            if (context.UserFoodFavs.Where(p => p.UserFoodFavID == userFoodFavsId).Count() != 0) {
                context.DeleteObject(context.UserFoodFavs.Where(p => p.UserFoodFavID == userFoodFavsId).First());
                context.SaveChanges();
            }
        }

        public List<Model.Food> GetAllUserFavsFood(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Food> food = new List<Model.Food>();
            List<Model.UserFoodFav> f=  context.UserFoodFavs.Where(p => p.UserID == userId).ToList();
            foreach (Model.UserFoodFav fav in f) {
                if (food.Where(p => p.FoodID == fav.FoodID).Count() == 0) {
                    food.Add(fav.Food);
                   
                }
            }
            return food;
        }
    }
}